﻿using AdminStyem.Application.Command.SlaughterCommand;
using AdminStyem.Domain.Entity.Boss;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.SlaughterCommandHandler
{
    public class SlaughterBassAddCommandHandler : IRequestHandler<SlaughterBassAddCommand, APIResult<object>>
    {
        IBaseRespository<Slaughteringfirm> BaseSlaughfirm;
        IMapper Mapper;
        public SlaughterBassAddCommandHandler(IBaseRespository<Slaughteringfirm> baseSlaughfirm, IMapper mapper)
        {
            BaseSlaughfirm = baseSlaughfirm;
            Mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(SlaughterBassAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret = new APIResult<object>();

            try
            {
                var entity = Mapper.Map<Slaughteringfirm>(request);
                entity.AddTime = DateTime.Now;
                await BaseSlaughfirm.add(entity);

                ret.Code = ResultCode.添加成功;
                ret.Message = ResultCode.添加成功.ToString();
            }
            catch (Exception ex)
            {
                ret.Code = ResultCode.添加失败;
                ret.Message = ex.Message;
            }

            return ret;
        }
    }
}
